package com.wanxi.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.Dao.HFMDao;
import com.wanxi.Dao.VLNDao;
import com.wanxi.pojo.Ledger;
import com.wanxi.pojo.MMjson;
import com.wanxi.pojo.MonthIE;
import com.wanxi.pojo.User;
import com.wanxi.pojo.query.LedgerQuery;
import com.wanxi.pojo.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HFMServiceImp implements HFMService {
    private HFMDao HFMDao;
    private VLNDao VLNDao;

    @Autowired
    public HFMServiceImp(HFMDao HFMDao, VLNDao VLNDao) {
        this.HFMDao = HFMDao;
        this.VLNDao = VLNDao;
    }


    @Override
    public boolean login(int userID, String userpassword) {
        //验证用户是否存在
        User tuser = HFMDao.queryUserId(userID);
        if (tuser == null) {
            System.out.println("没有对应用户！");
            return false;
        }
        // 验证密码是否正确
        if (!tuser.getUserpassword().equals(userpassword)) {
            System.out.println("密码错误");
            return false;
        }
        // 用户存在且密码正确
        return true;
    }

    @Override
    public List<String> getIEName() {
        return HFMDao.getIEName();
    }

    @Override
    public List<String> getCardBank(int userid) {
        return HFMDao.getCardBank(userid);
    }

    @Override
    public PageInfo<Ledger> ledger(LedgerQuery ledgerQuery, int userID) {
        User tuser = HFMDao.queryUserId(userID);
        //如果是root
        if (tuser.getUsertype().equals("root")) {
            PageHelper.startPage(ledgerQuery.getPageNum(), ledgerQuery.getPageSize());
            return new PageInfo<Ledger>(HFMDao.rootledger(ledgerQuery));
        }
        //是普通用户
        else {
            PageHelper.startPage(ledgerQuery.getPageNum(), ledgerQuery.getPageSize());
            return new PageInfo<Ledger>(HFMDao.ledger(ledgerQuery, userID));
        }
    }

    @Override
    public boolean addLedger(Ledger ledger, int userID) {
        int IEId = HFMDao.IEId(ledger.getIEName(), ledger.getIorE());
        int CardId = HFMDao.CardId(userID, ledger.getCardBank());
        return HFMDao.addLedger(ledger.getTime(), IEId, CardId, ledger.getMoney(), userID, ledger.getBeizhu());
    }

//    @Override
//    public List<User> userList() {
//        return HFMDao.userList();
//    }

    /**
     * 判断是否是root
     * @param userID
     * @return 是root返回true，不是返回false
     */
    @Override
    public boolean RorU(int userID) {
        User tuser = HFMDao.queryUserId(userID);
        if (tuser.getUsertype().equals("root")) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public PageInfo<User> userListByName(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        return new PageInfo<User>(HFMDao.userListByName(userQuery));
    }

    @Override
    public User queryUserId(int userid) {
        return HFMDao.queryUserId(userid);
    }

    @Override
    public boolean addUser(User user) {
        return HFMDao.addUser(user) > 0;
    }

    @Override
    public boolean upDateUser(User user) {
        return HFMDao.upDateUser(user) > 0;
    }

    @Override
    public boolean deleteUserById(Integer userID) {
        int a = HFMDao.ledgerDeleteUserById(userID);
        int b = HFMDao.usertableDeleteUserById(userID);
        if (a > 0) {
            System.out.println("删除用户的账单成功");
        } else
            System.out.println("删除用户的账单失败");
        return b > 0;
    }

    /**
     * <hr>
     * <br>
     * 以下为可视化
     * <br>
     * <br>
     * <hr>
     */
    @Override
    public List<String> listYear() {
        List<String> yearlist = VLNDao.listYear();
        return yearlist;
    }


    @Override
    public List<List<Integer>> yearMoney(int year) {
        List<List<Integer>> lists = new ArrayList<>();
        lists.add(VLNDao.getMon(year));
        lists.add(VLNDao.getIn(year));
        lists.add(VLNDao.getOut(year));
        return lists;
    }
    @Override
    public List<String> listMonth() {
        List<String> monthlist = VLNDao.listYear();
        return monthlist;
    }
    @Override
    public List<Integer> listUser() {
        List<Integer> userlist = VLNDao.listUser();
        return userlist;
    }

    @Override
    public List<MMjson> getMonthIE(MonthIE monthIE) {
        return VLNDao.getMonthIE(monthIE);
    }
}
